using System;
using UnityEngine;
using UnityEngine.UI;

public class CalendarController : MonoBehaviour
{
    public Text monthText;
    public Text topSelectedText;
    public Transform gridParent; // Calendar grid parent
    public GameObject dayPrefab; // DayCell prefab
    public Button prevBtn, nextBtn;
    public Color normalColor = Color.white;
    public Color todayColor = new Color(0.8f, 1f, 0.8f);
    public Color selectedColor = new Color(0.3f, 0.6f, 1f);
    public Color disabledColor = Color.gray;

    DateTime currentMonth;
    public DateTime selectedDateTime; // visible to other scripts

    DateTimePicker parentPicker;

    void Awake()
    {
        currentMonth = DateTime.Today;
        selectedDateTime = DateTime.Now;
    }

    void Start()
    {
        if (prevBtn) prevBtn.onClick.AddListener(()=> ChangeMonth(-1));
        if (nextBtn) nextBtn.onClick.AddListener(()=> ChangeMonth(1));
        GenerateCalendar();
    }

    public void SetParentPicker(DateTimePicker p)
    {
        parentPicker = p;
    }

    void ChangeMonth(int delta)
    {
        currentMonth = currentMonth.AddMonths(delta);
        GenerateCalendar();
    }

    void ClearGrid()
    {
        for (int i = gridParent.childCount - 1; i >= 0; i--)
            DestroyImmediate(gridParent.GetChild(i).gameObject);
    }

    public void GenerateCalendar()
    {
        ClearGrid();
        if (monthText) monthText.text = currentMonth.ToString("yyyy - MM");

        var firstDay = new DateTime(currentMonth.Year, currentMonth.Month, 1);
        int startIndex = (int)firstDay.DayOfWeek; // Sunday=0. If want Monday-first: ((int)firstDay.DayOfWeek + 6) % 7
        int days = DateTime.DaysInMonth(currentMonth.Year, currentMonth.Month);

        // Leading blanks
        for (int i = 0; i < startIndex; i++)
        {
            var blank = Instantiate(dayPrefab, gridParent);
            var txt = blank.GetComponentInChildren<Text>();
            if (txt) txt.text = "";
            var btn = blank.GetComponent<Button>();
            if (btn) btn.interactable = false;
            var img = blank.GetComponent<Image>();
            if (img) img.color = normalColor;
        }

        // Days
        for (int d = 1; d <= days; d++)
        {
            var go = Instantiate(dayPrefab, gridParent);
            var txt = go.GetComponentInChildren<Text>();
            if (txt) txt.text = d.ToString();
            var btn = go.GetComponent<Button>();
            DateTime thisDate = new DateTime(currentMonth.Year, currentMonth.Month, d);

            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(()=> OnDayClicked(thisDate));

            var img = go.GetComponent<Image>();
            bool disabled = false; // modify rule if needed
            if (disabled)
            {
                if (img) img.color = disabledColor;
                if (btn) btn.interactable = false;
            }
            else
            {
                if (thisDate.Date == DateTime.Today && img) img.color = todayColor;
                if (thisDate.Date == selectedDateTime.Date && img) img.color = selectedColor;
            }
        }
    }

    void OnDayClicked(DateTime date)
    {
        selectedDateTime = new DateTime(date.Year, date.Month, date.Day, selectedDateTime.Hour, selectedDateTime.Minute, 0);
        GenerateCalendar();
        parentPicker?.UpdateTopText();
    }

    public void SetSelectedDate(DateTime date)
    {
        selectedDateTime = new DateTime(date.Year, date.Month, date.Day, selectedDateTime.Hour, selectedDateTime.Minute, 0);
        currentMonth = new DateTime(selectedDateTime.Year, selectedDateTime.Month, 1);
        GenerateCalendar();
        parentPicker?.UpdateTopText();
    }

    public DateTime GetSelectedDateTime()
    {
        return selectedDateTime;
    }

    public void SetTime(int hour, int minute)
    {
        selectedDateTime = new DateTime(selectedDateTime.Year, selectedDateTime.Month, selectedDateTime.Day, hour, minute, 0);
        parentPicker?.UpdateTopText();
        GenerateCalendar();
    }
}